package com.time.oss.config;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 七牛云 SDK 配置
 */
@Configuration
public class QiniuConfig {

    @Value("${qiniu.accessKey}")
    private String accessKey;

    @Value("${qiniu.secretKey}")
    private String secretKey;

    @Value("${qiniu.region:auto}")
    private String region;

    @Bean
    public Auth qiniuAuth() {
        return Auth.create(accessKey, secretKey);
    }

    @Bean
    public com.qiniu.storage.Configuration qiniuConfiguration() {
        return new com.qiniu.storage.Configuration(resolveRegion(region));
    }

    @Bean
    public UploadManager uploadManager(com.qiniu.storage.Configuration cfg) {
        return new UploadManager(cfg);
    }

    @Bean
    public BucketManager bucketManager(Auth auth, com.qiniu.storage.Configuration cfg) {
        return new BucketManager(auth, cfg);
    }

    private Region resolveRegion(String regionName) {
        if (regionName == null) {
            return Region.autoRegion();
        }
        return switch (regionName.toLowerCase()) {
            case "huadong" -> Region.huadong();
            case "huabei" -> Region.huabei();
            case "huanan" -> Region.huanan();
            case "beimei" -> Region.beimei();
            case "xinjiapo" -> Region.xinjiapo();
            default -> Region.autoRegion();
        };
    }
}

